package com.cloudpivot.receivales;

import com.alibaba.fastjson.JSONObject;
import com.cloudpivot.receivales.ding.basic.controller.DingBasicController;
import com.cloudpivot.receivales.ding.basic.service.DingBasicService;
import com.cloudpivot.receivales.ding.basic.service.impl.HttpAPIServiceForDing;
import com.cloudpivot.receivales.ding.scenegroup.controller.CoolAppController;
import com.cloudpivot.receivales.ding.scenegroup.controller.ScenegroupController;
import com.cloudpivot.receivales.ding.scenegroup.domain.*;
import com.cloudpivot.receivales.ding.scenegroup.service.CrmDingService;
import com.cloudpivot.receivales.ding.scenegroup.service.DriveSpacesService;
import com.cloudpivot.receivales.ding.scenegroup.service.ScenegroupService;
import com.cloudpivot.receivales.ding.userinfo.service.DingUserService;
import org.apache.commons.lang.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;

import static com.cloudpivot.receivales.ding.util.DingUtil.listToString;

@SpringBootTest
class CloudpivotApplicationTestForDingScene {

    @Autowired
    private DingBasicService dingBasicService;
    @Autowired
    private DingUserService dingUserService;

    @Autowired
    private ScenegroupService scenegroupService;

    @Autowired
    private ScenegroupController scenegroupController;

    @Autowired
    private DriveSpacesService driveSpacesService;

    @Autowired
    private CrmDingService crmDingService;

    @Autowired
    private DingBasicController dingBasicController;
    @Autowired
    private HttpAPIServiceForDing httpAPIServiceForDing;

    @Autowired
    private CoolAppController coolAppController;

    @Test
    public void testForDingUser() {
        String phone = "15267167276";
        String userid = dingUserService.useridByMobilPhone(phone);
        System.out.println("userid:" + userid);
    }

    @Test
    public void testForDingUserunionid() {
        String phone = "15267167276";
        String unionid = dingUserService.getUnionidByMobilePhone(phone);
        System.out.println("userid:" + unionid);
    }

    @Test
    public void testForDingDriverSpaces() {
        String unionid = dingUserService.getUnionidByMobilePhone("15267167276");
        DriveSpacesDTO entity = new DriveSpacesDTO();
        entity.setName("铁三角订盘");
        entity.setUnionId(unionid);
        driveSpacesService.createDriveSpaces(entity);
    }

    /**
     * 测试 创建场景群
     */
    @Test
    public void testCreateScenegroup() {
        //定义 使用哪个模版
        //最新的模版
        String groupId = "0636c0be-3504-4a21-bad7-0abefe8bedd5";
        ScenegroupDTO scenegroup = new ScenegroupDTO();
        scenegroup.setTemplate_id(groupId);
        scenegroup.setTitle("营销任务场景群20220613");
        String ownerUser = dingUserService.useridByMobilPhone("15267167276");
        List<String> idList = new ArrayList<>();
        String[] usersPhone = {"17606715398", "17710024537"};
        for (String s : usersPhone) {
            String _s = dingUserService.useridByMobilPhone(s);
            if (StringUtils.isNotBlank(_s)) {
                idList.add(_s);
            }
        }
        idList.add(ownerUser);
        scenegroup.setOwner_user_id(ownerUser);
        scenegroup.setUser_ids(listToString(idList));
        scenegroupService.createScenegroup(scenegroup);
    }

    @Test
    public void testAddUserToScenegroup() {
        List<String> list = new ArrayList<>();
        list.add("17710024537");
        List<String> _list = dingUserService.convertListPhoneToUserid(list);
        AddUserToScenegroupDTO entity = new AddUserToScenegroupDTO();
        entity.setScenegroupId("cidwilCQjYpt+8LAnr4uy8Njg==");
        entity.setUser_ids(listToString(_list));
        scenegroupService.addUserToScenegroup(entity);
    }

    /**
     * 注册钉钉http回调
     */
    @Test
    public void testRegisterDingCallBack() {
//        dingBasicController.registerCallBack();
//        dingBasicController.getCallBack();
    }

    /**
     * 注册互动卡片回调
     */
    @Test
    public void testRegisterCardCallback() {
        JSONObject jsonObject = new JSONObject();
        //正式环境的回调
        jsonObject.put("url", "http://221.12.6.180:18085/saleapi/scenegroup/cardCallBack");
        //测试环境的回调
//        jsonObject.put("url", "http://123.157.219.35:18085/saleapi/scenegroup/cardCallBack");

        jsonObject.put("flag", "true");
        scenegroupController.registerCardCallBack(jsonObject);
    }

    /**
     * 创建互动卡片
     */
    @Test
    public void testCreateInteractiveCard() {
        InteractiveCardDTO.CardData carParam = new InteractiveCardDTO.CardData();
        Map<String, String> map1 = new HashMap<>();
        map1.put("managertaskid", "new-001-0001");
        map1.put("content", "这是api推送的互动卡片");
        carParam.setCardParamMap(map1);
        InteractiveCardDTO.CarOptions carOptions = new InteractiveCardDTO.CarOptions();
        carOptions.setSupportForward(true);
        InteractiveCardDTO entity = new InteractiveCardDTO();
        entity.setCardTemplateId("04766f4a-dcf6-4cc8-aeb8-499d81bb4a94");
        entity.setOpenConversationId("cidwilCQjYpt+8LAnr4uy8Njg==");
        entity.setOutTrackId("hd001");
        entity.setConversationType(1);
        entity.setCallbackRouteKey("dinginvesyj5cvqe41lh"); //中台场景群的appkey
        entity.setCardData(carParam);
        entity.setRobotCode("h8LI8HEw9B46AO216529522224891033");
        entity.setUserIdType(1);
        entity.setAtOpenIds(new HashMap<String, String>().put("@ALL", "@ALL"));   //默认都是全员接受
        entity.setCardOptions(carOptions);
        scenegroupService.createInteractiveCard(entity);
    }

    /**
     * 创建 机器人发送信息
     */
    @Test
    public void testCreateGroupRobot() {
        GroupRobotDTO entity = new GroupRobotDTO();
        entity.setTargetOpenConversationId("cidwilCQjYpt+8LAnr4uy8Njg==");
        entity.setMsgTemplateId("inner_app_template_markdown");
        JSONObject msgTemp = new JSONObject();
        msgTemp.put("markdown_content", "机器人 API 推送信息");
        msgTemp.put("title", "来自机器人的提醒");
        entity.setMsgParamMap(msgTemp.toJSONString());
        entity.setIsAtAll(true);
        entity.setRobotCode("h8LI8HEw9B46AO216529522224891033");
        scenegroupService.sendMsgByGroupRobot(entity);
    }

    /**
     * 测试crm 任务营销记录 转 钉钉任务卡片
     */
    @Test
    public void testGetCrmTaskmanager() {
        String guid = "eac94223-b8df-ec11-8ac9-fd3d50f160fd";
        String openid = "cidJEEj0lmPX51GXORZW8Mt7w==";
        TaskManagerDTO entity = crmDingService.getManagerTaskByGuid(guid);
        System.out.println(JSONObject.toJSONString(entity));
        InteractiveCardDTO cardEntity = crmDingService.convertInteractiveCardDTO(entity, openid);
        scenegroupService.createInteractiveCard(cardEntity);
    }

    /**
     * 测试 跳转card 推送的信息需要免登录
     */
    @Test
    public void testPushjumpcard() {
        String openid = "cidvh8cU134nxPLQNgJdLNUIQ==";
        ProjectShowDTO entity = new ProjectShowDTO();
        entity.setTitle("title");
        entity.setDesc("测试免登");
        entity.setLink("dingtalk://dingtalkclient/action/openapp?corpid=ding22251ff566ec9921&container_type=slide_panel&app_id=0_1647624984&redirect_type=jump&redirect_url=http%3A%2F%2Fbpm.biosan.cn%2Fmobile%3FcorpId%3Dding22251ff566ec9921%26agentId%3D1247202767%23%2Fform%2Fdetail%3FsheetCode%3Dsqcpfa%26objectId%3D7334bcc6790b4cc19b81ac9d4677335e%26schemaCode%3Dsqcpfa%26isWorkFlow%3Dfalse%26return%3D%252Fapps%252Fapps-form-list%252Fsqcpfa");
        entity.setPc_link("dingtalk://dingtalkclient/action/openapp?corpid=ding22251ff566ec9921&container_type=slide_panel&app_id=0_1647624984&redirect_type=jump&redirect_url=http%3A%2F%2Fbpm.biosan.cn%2Fmobile%3FcorpId%3Dding22251ff566ec9921%26agentId%3D1247202767%23%2Fform%2Fdetail%3FsheetCode%3Dsqcpfa%26objectId%3D7334bcc6790b4cc19b81ac9d4677335e%26schemaCode%3Dsqcpfa%26isWorkFlow%3Dfalse%26return%3D%252Fapps%252Fapps-form-list%252Fsqcpfa");
        InteractiveCardDTO cardEntity = crmDingService.convertToProjectShowDTO(entity, openid);
        scenegroupService.createInteractiveCard(cardEntity);
    }

    /**
     * 测试 跳转card
     */
    @Test
    public void testPushjumpcardForUrl() {
//        String[] users = new String[]{"020722623423157211"};
//        UrlCardDTO entity = new UrlCardDTO();
//        entity.setTitle("这个是标题");
//        entity.setDesc("这个是描述");
//        entity.setLink("https://www.baidu.com");
//        entity.setPc_link("https://www.baidu.com");
////        entity.setUserArray(users);
//        String ret=scenegroupController.pushUrlNormalCard(entity);
//        System.out.println(ret);
    }

    /**
     * 测试 模拟互动卡片的回调 (更新互动卡片)
     * 收到 的 测试
     */
    @Test
    public void testCallbackcard1() {
        JSONObject reqJson = new JSONObject();
        JSONObject privateData = new JSONObject();
        JSONObject cardOptions = new JSONObject();
        JSONObject paramMap = new JSONObject();
        JSONObject cardParamMap = new JSONObject();
        JSONObject cardData = new JSONObject();
        cardOptions.put("updateCardDataByKey", false);
        cardOptions.put("updatePrivateDataByKey", true);
        paramMap.put("checkflag", "true");
        cardParamMap.put("cardParamMap",paramMap);
        cardData.put("cardParamMap", paramMap);
//        privateData.put("userId", "020722623423157211");
        privateData.put("020722623423157211", cardParamMap);

        reqJson.put("carOptions", cardOptions);
        reqJson.put("privateData", privateData);
//        reqJson.put("cardData", cardData);
        reqJson.put("outTrackId", "e3d1ae2b-0a34-401c-934f-5a6557b15afa");
        reqJson.put("userIdType", 1);
        String accessToken = dingBasicService.getToken("dinggroup");
        String url = "https://api.dingtalk.com/v1.0/im/interactiveCards";
        System.out.println("请求参数："+reqJson.toJSONString());
        String ret = null;
        try {
            ret = httpAPIServiceForDing.doPutDing(url, JSONObject.toJSONString(reqJson), accessToken);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("resp:"+ret);
    }

    /**
     * 测试 模拟互动卡片的回调 (更新互动卡片)
     * 评论 的 测试
     */
    @Test
    public void testCallbackcard_pinglun() {
        JSONObject reqJson = new JSONObject();
        JSONObject cardOptions = new JSONObject();
        JSONObject paramMap = new JSONObject();
        JSONObject cardParamMap = new JSONObject();
        JSONObject privateData = new JSONObject();
        cardOptions.put("updateCardDataByKey", true);
//        cardOptions.put("updatePrivateDataByKey", true);
        paramMap.put("pinglun", "这个是痞子姚的评论!!!!!");
        cardParamMap.put("cardParamMap",paramMap);
        privateData.put("020722623423157211", cardParamMap);

//        reqJson.put("privateData", privateData);
        reqJson.put("carOptions", cardOptions);
        reqJson.put("cardData", cardParamMap);
        reqJson.put("outTrackId", "2ed284e6-a766-4836-9fdf-2500b4caf1ea");
//        reqJson.put("userIdType", 1);
        String accessToken = dingBasicService.getToken("dinggroup");
        String url = "https://api.dingtalk.com/v1.0/im/interactiveCards";
        System.out.println("请求参数："+reqJson.toJSONString());

        String ret = null;
        try {
            ret = httpAPIServiceForDing.doPutDing(url, JSONObject.toJSONString(reqJson), accessToken);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("resp:"+ret);
    }

    @Test
    public void testSendFileToGroup() {
        String chatid = "chat59e0a14c4deb0b1267b60f42623d7055";
        String mediaid = "@lAjPDe7s4XGbL5POBQclbc5BNwIG";
        scenegroupService.sendMsgByPost(chatid,mediaid);
    }

    @Test
    public void test111() {
        String str = "abc";
        String[] strs = str.split(",");
        for (String s : strs) {
            System.out.println(s);
        }
    }
}
